﻿using System;
using System.Data.SqlClient;
using DataBaseHelper.Properties;

namespace DataBaseHelper
{
    public class DBMSException : Exception
    {
        public DBMSException(Exception ex)
            : base(R.DBExcptionMSG, ex)
        {
            this.HelpLink = "";
        }

        public override string ToString()
        {
            if (typeof(SqlException).IsInstanceOfType(InnerException))
            {
                SqlException ex = InnerException as SqlException;
                if (ex != null)
                {
                    return String.Format("{0}\r\nFor Debug:{{ Code = 0x{1:X8} }}", Message, ex.Number);
                }                
            }
            else
            {
                return String.Format("{0}\r\nFor Debug:{{ InnerMessage = {1} }}", Message, InnerException.Message);
            }
            return base.ToString();
        }
    }

    public class DBMSParamException : ArgumentException
    {
        public DBMSParamException(string paramName, Exception ex)
            : base(R.DBParamExcptionMSG, paramName, ex)
        {
            this.HelpLink = "";
        }

        public override string ToString()
        {
            if (typeof(SqlException).IsInstanceOfType(InnerException))
            {
                SqlException ex = InnerException as SqlException;
                if (ex != null)
                {
                    return String.Format("{0}\r\nFor Debug:{{ Name = {2},Code = 0x{1:X8} }}", Message, ex.Number, ParamName);
                }
            }
            else
            {
                return String.Format("{0}\r\nFor Debug:{{ InnerMessage = {1} }}", Message, InnerException.Message);
            }
            return base.ToString();
        }
    }
}
